#!/bin/bash -e

opts="clone clone_backend backend_install backend_package backend_start backend_stop h5_install h5_dev h5build\
 server_eureka_package server_config_package server_api_gateway_package service_basic_package service_user_package\
 service_order_package service_supply_package service_purchase_package service_payment_package\
 service_job_package service_message_package create_branch
"

WORKSPACE=${WORKSPACE:-$(pwd)}
ENV=${ENV:-"local"}
BRANCH=${BRANCH:-"master"}
BACKEND_VERSION="1.0.0"
JAVA_OPTS=${JAVA_OPTS:-"-Xms32m -Xmx64m"}

init() {
  echo "=============================================="
  echo "Init project"
  echo "=============================================="

}

clone() {
  echo "=============================================="
  echo "Clone project with branch $BRANCH"
  echo "=============================================="

  clone_h5
  clone_portal
  clone_monitor
  clone_pc
  clone_app
  clone_backend
  clone_expo
  clone_abchinafront
  clone_migration

  echo "=============================================="
  echo "Clone project done."
  echo "=============================================="
}

clone_h5() {
  echo -e "\nClone h5"
  echo "----------------------------------------------"
  if [ ! -e $WORKSPACE/h5 ]; then
    git clone git@dev.zgyn.net:cisdi-yn-farmer/h5.git -b $BRANCH
  fi
}

clone_portal() {
  echo -e "\nClone portal"
  echo "----------------------------------------------"
  if [ ! -e $WORKSPACE/portal ]; then
    git clone git@dev.zgyn.net:cisdi-yn-farmer/portal.git -b $BRANCH
  fi
}

clone_monitor() {
  echo -e "\nClone monitor"
  echo "----------------------------------------------"
  if [ ! -e $WORKSPACE/monitor ]; then
    git clone git@dev.zgyn.net:cisdi-yn-farmer/monitor.git -b $BRANCH
  fi
}

clone_pc() {
  echo -e "\nClone pc"
  echo "----------------------------------------------"
  if [ ! -e $WORKSPACE/pc ]; then
    git clone git@dev.zgyn.net:cisdi-yn-farmer/pc.git -b $BRANCH
  fi
}

clone_app() {
  echo -e "\nClone app"
  echo "----------------------------------------------"
  if [ ! -e $WORKSPACE/app ]; then
    git clone git@dev.zgyn.net:cisdi-yn-farmer/app.git -b $BRANCH
  fi
}

clone_expo() {
  echo -e "\nClone expo"
  echo "----------------------------------------------"
  if [ ! -e $WORKSPACE/expo ]; then
    git clone git@dev.zgyn.net:cisdi-yn-farmer/expo.git -b $BRANCH
  fi
}

clone_abchinafront() {
  echo -e "\nClone abchinafront"
  echo "----------------------------------------------"
  if [ ! -e $WORKSPACE/abchinafront ]; then
    git clone git@dev.zgyn.net:cisdi-yn-farmer/abchinafront.git -b $BRANCH
  fi
}

clone_backend() {
  echo -e "\nClone backend"
  echo "----------------------------------------------"

  [ ! -e $WORKSPACE/backend ] && git clone git@dev.zgyn.net:cisdi-yn-farmer/backend.git -b $BRANCH

  echo -e "\nClone lib-common\n"
  [ ! -e $WORKSPACE/backend/libs/lib-common ] && git clone git@dev.zgyn.net:cisdi-yn-farmer/lib-common.git -b $BRANCH $WORKSPACE/backend/libs/lib-common

  echo -e "\nClone server-eureka\n"
  [ ! -e $WORKSPACE/backend/servers/server-eureka ] && git clone git@dev.zgyn.net:cisdi-yn-farmer/server-eureka.git -b $BRANCH $WORKSPACE/backend/servers/server-eureka
  echo -e "\nClone server-apollo\n"
  [ ! -e $WORKSPACE/backend/servers/server-apollo ] && git clone git@dev.zgyn.net:cisdi-yn-farmer/server-apollo.git -b $BRANCH $WORKSPACE/backend/servers/server-apollo
  echo -e "\nClone server-config\n"
  [ ! -e $WORKSPACE/backend/servers/server-config ] && git clone git@dev.zgyn.net:cisdi-yn-farmer/server-config.git -b $BRANCH $WORKSPACE/backend/servers/server-config
  echo -e "\nClone server-api-gateway\n"
  [ ! -e $WORKSPACE/backend/servers/server-api-gateway ] && git clone git@dev.zgyn.net:cisdi-yn-farmer/server-api-gateway.git -b $BRANCH $WORKSPACE/backend/servers/server-api-gateway
  echo -e "\nClone server-admin\n"
  [ ! -e $WORKSPACE/backend/servers/server-admin ] && git clone git@dev.zgyn.net:cisdi-yn-farmer/server-admin.git -b $BRANCH $WORKSPACE/backend/servers/server-admin

  echo -e "\nClone service-basic\n"
  [ ! -e $WORKSPACE/backend/services/service-basic ] && git clone git@dev.zgyn.net:cisdi-yn-farmer/service-basic.git -b $BRANCH $WORKSPACE/backend/services/service-basic
  echo -e "\nClone service-user\n"
  [ ! -e $WORKSPACE/backend/services/service-user ] && git clone git@dev.zgyn.net:cisdi-yn-farmer/service-user.git -b $BRANCH $WORKSPACE/backend/services/service-user
  echo -e "\nClone service-order\n" 
  [ ! -e $WORKSPACE/backend/services/service-order ] && git clone git@dev.zgyn.net:cisdi-yn-farmer/service-order.git -b $BRANCH $WORKSPACE/backend/services/service-order
  echo -e "\nClone service-job\n" 
  [ ! -e $WORKSPACE/backend/services/service-job ] && git clone git@dev.zgyn.net:cisdi-yn-farmer/service-job.git -b $BRANCH $WORKSPACE/backend/services/service-job
  echo -e "\nClone service-supply\n" 
  [ ! -e $WORKSPACE/backend/services/service-supply ] && git clone git@dev.zgyn.net:cisdi-yn-farmer/service-supply.git -b $BRANCH $WORKSPACE/backend/services/service-supply
  echo -e "\nClone service-payment\n" 
  [ ! -e $WORKSPACE/backend/services/service-payment ] && git clone git@dev.zgyn.net:cisdi-yn-farmer/service-payment.git -b $BRANCH $WORKSPACE/backend/services/service-payment
  echo -e "\nClone service-identify\n" 
  [ ! -e $WORKSPACE/backend/services/service-identify ] && git clone git@dev.zgyn.net:cisdi-yn-farmer/service-identify.git -b $BRANCH $WORKSPACE/backend/services/service-identify
  echo -e "\nClone service-message\n" 
  [ ! -e $WORKSPACE/backend/services/service-message ] && git clone git@dev.zgyn.net:cisdi-yn-farmer/service-message.git -b $BRANCH $WORKSPACE/backend/services/service-message
  echo -e "\nClone service-purchase\n"
  if [ ! -e $WORKSPACE/backend/services/service-purchase ]; then
    git clone git@dev.zgyn.net:cisdi-yn-farmer/service-purchase.git -b $BRANCH $WORKSPACE/backend/services/service-purchase
  fi
}

clone_migration() {
  echo -e "\nClone migration"
  echo "----------------------------------------------"
  if [ ! -e $WORKSPACE/migration ]; then
    git clone git@dev.zgyn.net:cisdi-yn-farmer/migration.git -b $BRANCH
  fi
}

create_branch() {
  echo "=============================================="
  echo "Create branch $BRANCH"
  echo "=============================================="

  create_branch_h5
  create_branch_portal
  create_branch_monitor
  create_branch_pc
  create_branch_app
  create_branch_backend
  create_branch_expo
  create_branch_abchinafront
  create_branch_migration

  echo "=============================================="
  echo "Create branch done."
  echo "=============================================="
}

create_branch_h5() {
  echo -e "\ncreate branch h5"
  echo "----------------------------------------------"
  if [ -e $WORKSPACE/h5 ]; then
    cd $WORKSPACE/h5 && git checkout -b $BRANCH && git push origin $BRANCH
  fi
}

create_branch_portal() {
  echo -e "\ncreate branch portal"
  echo "----------------------------------------------"
  if [ -e $WORKSPACE/portal ]; then
    cd $WORKSPACE/portal && git checkout -b $BRANCH && git push origin $BRANCH
  fi
}

create_branch_monitor() {
  echo -e "\ncreate branch monitor"
  echo "----------------------------------------------"
  if [ -e $WORKSPACE/monitor ]; then
    cd $WORKSPACE/monitor && git checkout -b $BRANCH && git push origin $BRANCH
  fi
}

create_branch_pc() {
  echo -e "\ncreate branch pc"
  echo "----------------------------------------------"
  if [ -e $WORKSPACE/pc ]; then
    cd $WORKSPACE/pc && git checkout -b $BRANCH && git push origin $BRANCH
  fi
}

create_branch_app() {
  echo -e "\ncreate branch app"
  echo "----------------------------------------------"
  if [ -e $WORKSPACE/app ]; then
    cd $WORKSPACE/app && git checkout -b $BRANCH && git push origin $BRANCH
  fi
}

create_branch_expo() {
  echo -e "\ncreate branch expo"
  echo "----------------------------------------------"
  if [ -e $WORKSPACE/expo ]; then
    cd $WORKSPACE/expo && git checkout -b $BRANCH && git push origin $BRANCH
  fi
}

create_branch_abchinafront() {
  echo -e "\ncreate branch abchinafront"
  echo "----------------------------------------------"
  if [ -e $WORKSPACE/abchinafront ]; then
    cd $WORKSPACE/abchinafront && git checkout -b $BRANCH && git push origin $BRANCH
  fi
}

create_branch_backend() {
  echo -e "\ncreate branch backend"
  echo "----------------------------------------------"

  [ -e $WORKSPACE/backend ] && (cd $WORKSPACE/backend && git checkout -b $BRANCH && git push origin $BRANCH)

  echo -e "\ncreate branch lib-common\n"
  [ -e $WORKSPACE/backend/libs/lib-common ] && (cd $WORKSPACE/backend/libs/lib-common && git checkout -b $BRANCH && git push origin $BRANCH)

  echo -e "\ncreate branch server-eureka\n"
  [ -e $WORKSPACE/backend/servers/server-eureka ] && (cd $WORKSPACE/backend/servers/server-eureka && git checkout -b $BRANCH && git push origin $BRANCH)
  echo -e "\ncreate branch server-config\n"
  [ -e $WORKSPACE/backend/servers/server-config ] && (cd $WORKSPACE/backend/servers/server-config && git checkout -b $BRANCH && git push origin $BRANCH)
  echo -e "\ncreate branch server-api-gateway\n"
  [ -e $WORKSPACE/backend/servers/server-api-gateway ] && (cd $WORKSPACE/backend/servers/server-api-gateway && git checkout -b $BRANCH && git push origin $BRANCH)
  echo -e "\ncreate branch server-admin\n"
  [ -e $WORKSPACE/backend/servers/server-admin ] && (cd $WORKSPACE/backend/servers/server-admin && git checkout -b $BRANCH && git push origin $BRANCH)
  echo -e "\ncreate branch server-apollo\n"
  [ -e $WORKSPACE/backend/servers/server-apollo ] && (cd $WORKSPACE/backend/servers/server-apollo && git checkout -b $BRANCH && git push origin $BRANCH)

  echo -e "\ncreate branch service-basic\n"
  [ -e $WORKSPACE/backend/services/service-basic ] && (cd $WORKSPACE/backend/services/service-basic && git checkout -b $BRANCH && git push origin $BRANCH)
  echo -e "\ncreate branch service-user\n"
  [ -e $WORKSPACE/backend/services/service-user ] && (cd $WORKSPACE/backend/services/service-user && git checkout -b $BRANCH && git push origin $BRANCH)
  echo -e "\ncreate branch service-order\n" 
  [ -e $WORKSPACE/backend/services/service-order ] && (cd $WORKSPACE/backend/services/service-order && git checkout -b $BRANCH && git push origin $BRANCH)
  echo -e "\ncreate branch service-job\n" 
  [ -e $WORKSPACE/backend/services/service-job ] && (cd $WORKSPACE/backend/services/service-job && git checkout -b $BRANCH && git push origin $BRANCH)
  echo -e "\ncreate branch service-supply\n" 
  [ -e $WORKSPACE/backend/services/service-supply ] && (cd $WORKSPACE/backend/services/service-supply && git checkout -b $BRANCH && git push origin $BRANCH)
  echo -e "\ncreate branch service-payment\n" 
  [ -e $WORKSPACE/backend/services/service-payment ] && (cd $WORKSPACE/backend/services/service-payment && git checkout -b $BRANCH && git push origin $BRANCH)
  echo -e "\ncreate branch service-message\n" 
  [ -e $WORKSPACE/backend/services/service-message ] && (cd $WORKSPACE/backend/services/service-message && git checkout -b $BRANCH && git push origin $BRANCH)
  echo -e "\ncreate branch service-identify\n" 
  [ -e $WORKSPACE/backend/services/service-identify ] && (cd $WORKSPACE/backend/services/service-identify && git checkout -b $BRANCH && git push origin $BRANCH)
  echo -e "\ncreate branch service-purchase\n"
  if [ -e $WORKSPACE/backend/services/service-purchase ]; then
    cd $WORKSPACE/backend/services/service-purchase && git checkout -b $BRANCH && git push origin $BRANCH
  fi
}

create_branch_migration() {
  echo -e "\ncreate branch migration"
  echo "----------------------------------------------"
  if [ -e $WORKSPACE/migration ]; then
    cd $WORKSPACE/migration && git checkout -b $BRANCH && git push origin $BRANCH
  fi
}


server_eureka_package() {
  echo "=============================================="
  echo "Package server-eureka"
  echo "=============================================="

  backend_install

  echo -e "\npackage server-eureka\n"
  cd $WORKSPACE/backend/servers/server-eureka && mvn clean package -P $ENV

  echo "=============================================="
  echo "Package server-eureka done."
  echo "=============================================="
}

server_config_package() {
  echo "=============================================="
  echo "Package server-config"
  echo "=============================================="

  backend_install

  echo -e "\npackage server-config\n"
  cd $WORKSPACE/backend/servers/server-config && mvn clean package -P $ENV

  echo "=============================================="
  echo "Package server-config done."
  echo "=============================================="
}

server_api_gateway_package() {
  echo "=============================================="
  echo "Package server-api-gateway"
  echo "=============================================="

  backend_install

  echo -e "\npackage server-api-gateway\n"
  cd $WORKSPACE/backend/servers/server-api-gateway && mvn clean package -P $ENV

  echo "=============================================="
  echo "Package server-api-gateway done."
  echo "=============================================="
}

service_basic_package() {
  echo "=============================================="
  echo "Package service-basic"
  echo "=============================================="

  backend_install

  echo -e "\npackage service-basic\n"
  cd $WORKSPACE/backend/services/service-basic && mvn clean package -P $ENV

  echo "=============================================="
  echo "Package service-basic done."
  echo "=============================================="
}

service_user_package() {
  echo "=============================================="
  echo "Package service-user"
  echo "=============================================="

  backend_install

  echo -e "\npackage service-user\n"
  cd $WORKSPACE/backend/services/service-user && mvn clean package -P $ENV

  echo "=============================================="
  echo "Package service-user done."
  echo "=============================================="
}

service_order_package() {
  echo "=============================================="
  echo "Package service-order"
  echo "=============================================="

  backend_install

  echo -e "\npackage service-order\n"
  cd $WORKSPACE/backend/services/service-order && mvn clean package -P $ENV

  echo "=============================================="
  echo "Package service-order done."
  echo "=============================================="
}

service_supply_package() {
  echo "=============================================="
  echo "Package service-supply"
  echo "=============================================="

  backend_install

  echo -e "\npackage service-supply\n"
  cd $WORKSPACE/backend/services/service-supply && mvn clean package -P $ENV

  echo "=============================================="
  echo "Package service-supply done."
  echo "=============================================="
}

service_payment_package() {
  echo "=============================================="
  echo "Package service-payment"
  echo "=============================================="

  backend_install

  echo -e "\npackage service-payment\n"
  cd $WORKSPACE/backend/services/service-payment && mvn clean package -P $ENV

  echo "=============================================="
  echo "Package service-payment done."
  echo "=============================================="
}

service_purchase_package() {
  echo "=============================================="
  echo "Package service-purchase"
  echo "=============================================="

  backend_install

  echo -e "\npackage service-purchase\n"
  cd $WORKSPACE/backend/services/service-purchase && mvn clean package -P $ENV

  echo "=============================================="
  echo "Package service-purchase done."
  echo "=============================================="
}

service_job_package() {
  echo "=============================================="
  echo "Package service-job"
  echo "=============================================="

  backend_install

  echo -e "\npackage service-job\n"
  cd $WORKSPACE/backend/services/service-job && mvn clean package -P $ENV

  echo "=============================================="
  echo "Package service-job done."
  echo "=============================================="
}

service_message_package() {
  echo "=============================================="
  echo "Package service-message"
  echo "=============================================="

  backend_install

  echo -e "\npackage service-message\n"
  cd $WORKSPACE/backend/services/service-message && mvn clean package -P $ENV

  echo "=============================================="
  echo "Package service-message done."
  echo "=============================================="
}

backend_install() {
  echo "=============================================="
  echo "Install backend"
  echo "=============================================="

  cd $WORKSPACE/backend && mvn clean install

  echo "=============================================="
  echo "Install backend done."
  echo "=============================================="
}

backend_package() {
  echo "=============================================="
  echo "Package backend"
  echo "=============================================="

  backend_install

  echo -e "\npackage server-eureka\n"
  cd $WORKSPACE/backend/servers/server-eureka && mvn clean package -P $ENV

  echo -e "\npackage server-config\n"
  cd $WORKSPACE/backend/servers/server-config && mvn clean package -P $ENV

  echo -e "\npackage server-api-gateway\n"
  cd $WORKSPACE/backend/servers/server-api-gateway && mvn clean package -P $ENV

  echo -e "\npackage server-admin\n"
  cd $WORKSPACE/backend/servers/server-admin && mvn clean package -P $ENV

  echo -e "\npackage service-basic\n"
  cd $WORKSPACE/backend/services/service-basic && mvn clean package -P $ENV

  echo -e "\npackage service-user\n"
  cd $WORKSPACE/backend/services/service-user && mvn clean package -P $ENV

  echo -e "\npackage service-order\n"
  cd $WORKSPACE/backend/services/service-order && mvn clean package -P $ENV

  echo -e "\npackage service-purchase\n"
  cd $WORKSPACE/backend/services/service-purchase && mvn clean package -P $ENV

  echo -e "\npackage service-supply\n"
  cd $WORKSPACE/backend/services/service-supply && mvn clean package -P $ENV

  echo -e "\npackage service-payment\n"
  cd $WORKSPACE/backend/services/service-payment && mvn clean package -P $ENV

  echo -e "\npackage service-job\n"
  cd $WORKSPACE/backend/services/service-job && mvn clean package -P $ENV

  echo -e "\npackage service-message\n"
  cd $WORKSPACE/backend/services/service-message && mvn clean package -P $ENV

  echo "=============================================="
  echo "Package backend done."
  echo "=============================================="
}

backend_start() {
  echo "=============================================="
  echo "Start backend"
  echo "=============================================="

  backend_install
  backend_stop

  echo -e "\nStart server-eureka\n"
  cd $WORKSPACE/backend/servers/server-eureka && mvn clean package -P $ENV
  java $JAVA_OPTS -jar $WORKSPACE/backend/servers/server-eureka/target/server-eureka-$BACKEND_VERSION-RELEASE.jar > /dev/null 2>&1 &
  disown "%java"

  echo -e "\nStart server-config\n"
  cd $WORKSPACE/backend/servers/server-config && mvn clean package -P $ENV
  java $JAVA_OPTS -jar $WORKSPACE/backend/servers/server-config/target/server-config-$BACKEND_VERSION-RELEASE.jar > /dev/null 2>&1 &
  disown "%java"

  echo -e "\nStart server-api-gateway\n"
  cd $WORKSPACE/backend/servers/server-api-gateway && mvn clean package -P $ENV
  java $JAVA_OPTS -jar $WORKSPACE/backend/servers/server-api-gateway/target/server-api-gateway-$BACKEND_VERSION-RELEASE.jar > /dev/null 2>&1 &
  disown "%java"

  echo -e "\nStart server-admin\n"
  cd $WORKSPACE/backend/servers/server-admin && mvn clean package -P $ENV
  java $JAVA_OPTS -jar $WORKSPACE/backend/servers/server-admin/target/server-admin-$BACKEND_VERSION-RELEASE.jar > /dev/null 2>&1 &
  disown "%java"

  echo -e "\nStart service-basic\n"
  cd $WORKSPACE/backend/services/service-basic && mvn clean package -P $ENV
  java $JAVA_OPTS -jar $WORKSPACE/backend/services/service-basic/service-basic-server/target/service-basic-server-$BACKEND_VERSION-RELEASE.jar > /dev/null 2>&1 &
  disown "%java"

  echo -e "\nStart service-user\n"
  cd $WORKSPACE/backend/services/service-user && mvn clean package -P $ENV
  java $JAVA_OPTS -jar $WORKSPACE/backend/services/service-user/service-user-server/target/service-user-server-$BACKEND_VERSION-RELEASE.jar > /dev/null 2>&1 &
  disown "%java"

  echo -e "\nStart service-order\n"
  cd $WORKSPACE/backend/services/service-order && mvn clean package -P $ENV
  java $JAVA_OPTS -jar $WORKSPACE/backend/services/service-order/service-order-server/target/service-order-server-$BACKEND_VERSION-RELEASE.jar > /dev/null 2>&1 &
  disown "%java"

  echo -e "\nStart service-purchase\n"
  cd $WORKSPACE/backend/services/service-purchase && mvn clean package -P $ENV
  java $JAVA_OPTS -jar $WORKSPACE/backend/services/service-purchase/service-purchase-server/target/service-purchase-server-$BACKEND_VERSION-RELEASE.jar > /dev/null 2>&1 &
  disown "%java"

  echo -e "\nStart service-supply\n"
  cd $WORKSPACE/backend/services/service-supply && mvn clean package -P $ENV
  java $JAVA_OPTS -jar $WORKSPACE/backend/services/service-supply/service-supply-server/target/service-supply-server-$BACKEND_VERSION-RELEASE.jar > /dev/null 2>&1 &
  disown "%java"

  echo -e "\nStart service-payment\n"
  cd $WORKSPACE/backend/services/service-payment && mvn clean package -P $ENV
  java $JAVA_OPTS -jar $WORKSPACE/backend/services/service-payment/service-payment-server/target/service-payment-server-$BACKEND_VERSION-RELEASE.jar > /dev/null 2>&1 &
  disown "%java"

  echo -e "\nStart service-job\n"
  cd $WORKSPACE/backend/services/service-job && mvn clean package -P $ENV
  java $JAVA_OPTS -jar $WORKSPACE/backend/services/service-job/service-job-server/target/service-job-server-$BACKEND_VERSION-RELEASE.jar > /dev/null 2>&1 &
  disown "%java"

  echo -e "\nStart service-message\n"
  cd $WORKSPACE/backend/services/service-message && mvn clean package -P $ENV
  java $JAVA_OPTS -jar $WORKSPACE/backend/services/service-message/service-message-server/target/service-message-server-$BACKEND_VERSION-RELEASE.jar > /dev/null 2>&1 &
  disown "%java"

  echo "=============================================="
  echo "Start backend done."
  echo "=============================================="
}

backend_stop() {

  pid=$(ps -ef | grep service-basic-server-$BACKEND_VERSION-RELEASE.jar | grep -v grep | awk '{print $2}')
  if [ -n "$pid" ]; then
    echo "Stop service-basic"
    ps -ef | grep service-basic-server-$BACKEND_VERSION-RELEASE.jar | grep -v grep | awk '{print $2}' | xargs kill -9
  fi

  pid=$(ps -ef | grep service-order-server-$BACKEND_VERSION-RELEASE.jar | grep -v grep | awk '{print $2}')
  if [ -n "$pid" ]; then
    echo "Stop service-order"
    ps -ef | grep service-order-server-$BACKEND_VERSION-RELEASE.jar | grep -v grep | awk '{print $2}' | xargs kill -9
  fi

  pid=$(ps -ef | grep service-purchase-server-$BACKEND_VERSION-RELEASE.jar | grep -v grep | awk '{print $2}')
  if [ -n "$pid" ]; then
    echo "Stop service-purchase"
    ps -ef | grep service-purchase-server-$BACKEND_VERSION-RELEASE.jar | grep -v grep | awk '{print $2}' | xargs kill -9
  fi

  pid=$(ps -ef | grep service-supply-server-$BACKEND_VERSION-RELEASE.jar | grep -v grep | awk '{print $2}')
  if [ -n "$pid" ]; then
    echo "Stop service-supply"
    ps -ef | grep service-supply-server-$BACKEND_VERSION-RELEASE.jar | grep -v grep | awk '{print $2}' | xargs kill -9
  fi

  pid=$(ps -ef | grep service-payment-server-$BACKEND_VERSION-RELEASE.jar | grep -v grep | awk '{print $2}')
  if [ -n "$pid" ]; then
    echo "Stop service-payment"
    ps -ef | grep service-payment-server-$BACKEND_VERSION-RELEASE.jar | grep -v grep | awk '{print $2}' | xargs kill -9
  fi

  pid=$(ps -ef | grep service-user-server-$BACKEND_VERSION-RELEASE.jar | grep -v grep | awk '{print $2}')
  if [ -n "$pid" ]; then
    echo "Stop service-user"
    ps -ef | grep service-user-server-$BACKEND_VERSION-RELEASE.jar | grep -v grep | awk '{print $2}' | xargs kill -9
  fi

  pid=$(ps -ef | grep service-job-server-$BACKEND_VERSION-RELEASE.jar | grep -v grep | awk '{print $2}')
  if [ -n "$pid" ]; then
    echo "Stop service-job"
    ps -ef | grep service-job-server-$BACKEND_VERSION-RELEASE.jar | grep -v grep | awk '{print $2}' | xargs kill -9
  fi

  pid=$(ps -ef | grep service-message-server-$BACKEND_VERSION-RELEASE.jar | grep -v grep | awk '{print $2}')
  if [ -n "$pid" ]; then
    echo "Stop service-message"
    ps -ef | grep service-message-server-$BACKEND_VERSION-RELEASE.jar | grep -v grep | awk '{print $2}' | xargs kill -9
  fi

  pid=$(ps -ef | grep server-admin-$BACKEND_VERSION-RELEASE.jar | grep -v grep | awk '{print $2}')
  if [ -n "$pid" ]; then
    echo "Stop server-admin"
    ps -ef | grep server-admin-$BACKEND_VERSION-RELEASE.jar | grep -v grep | awk '{print $2}' | xargs kill -9
  fi

  pid=$(ps -ef | grep server-api-gateway-$BACKEND_VERSION-RELEASE.jar | grep -v grep | awk '{print $2}')
  if [ -n "$pid" ]; then
    echo "Stop server-api-gateway"
    ps -ef | grep server-api-gateway-$BACKEND_VERSION-RELEASE.jar | grep -v grep | awk '{print $2}' | xargs kill -9
  fi

  pid=$(ps -ef | grep server-eureka-$BACKEND_VERSION-RELEASE.jar | grep -v grep | awk '{print $2}')
  if [ -n "$pid" ]; then
    echo "Stop server-eureka"
    ps -ef | grep server-eureka-$BACKEND_VERSION-RELEASE.jar | grep -v grep | awk '{print $2}' | xargs kill -9
  fi

  pid=$(ps -ef | grep server-config-$BACKEND_VERSION-RELEASE.jar | grep -v grep | awk '{print $2}')
  if [ -n "$pid" ]; then
    echo "Stop server-config"
    ps -ef | grep server-config-$BACKEND_VERSION-RELEASE.jar | grep -v grep | awk '{print $2}' | xargs kill -9
  fi
}

h5_install() {
  cd $WORKSPACE/h5/src && npm install
}

h5_dev() {
  cd $WORKSPACE/h5/src && NODE_ENV=$ENV npm run h5dev
}

h5_build() {
  cd $WORKSPACE/h5/src && NODE_ENV=$ENV npm run h5build
}

usage() {
  echo "USAGE: $0" option key

  echo -e "\nOptions:"
  for opt in $opts; do
    echo "    ${opt}"
  done
  echo -e ""
}


case "$1" in
  clone)
    clone
    ;;
  clone_backend)
    clone_backend
    ;;
  backend_install)
    backend_install
    ;;
  backend_package)
    backend_package
    ;;
  backend_start)
    backend_start
    ;;
  backend_stop)
    backend_stop
    ;;
  h5_build)
    h5_build
    ;;
  h5_dev)
    h5_dev
    ;;
  h5_install)
    h5_install
    ;;
  server_eureka_package)
    server_eureka_package
    ;;
  server_config_package)
    server_config_package
    ;;
  server_api_gateway_package)
    server_api_gateway_package
    ;;
  service_basic_package)
    service_basic_package
    ;;
  service_user_package)
    service_user_package
    ;;
  service_order_package)
    service_order_package
    ;;
  service_supply_package)
    service_supply_package
    ;;
  service_payment_package)
    service_payment_package
    ;;
  service_purchase_package)
    service_purchase_package
    ;;
  service_job_package)
    service_job_package
    ;;
  service_message_package)
    service_message_package
    ;;
  create_branch)
    create_branch
    ;;
  *)
    usage
    ;;
esac